package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.DateUtils;
import com.rongshu.view.discovery.bean.ArticleDetaisRequest;
import com.rongshu.view.home.activity.SingleArticleDetailsActivity;
import com.rongshu.view.penpal.activity.PenpalDetailActivity;
import com.rongshu.view.userInfo.activity.AddVCertificationActivity;
import com.rongshu.view.userInfo.activity.CardBagActivity;
import com.rongshu.view.userInfo.activity.FansActivity;
import com.rongshu.view.userInfo.activity.SystemMessageDetailsActivity;
import com.rongshu.view.userInfo.entity.SystemMessage;
import com.rongshu.view.userInfo.listener.OnMsgFunctionClickListener;
import com.widget.SwipeMenuView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SystemsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<SystemMessage.ResponseBean> list = null;
    //listener
    private OnMsgFunctionClickListener listener = null;
    private Context context;

    public SystemsAdapter(Context context, List<SystemMessage.ResponseBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setOnMsgFunctionListener(OnMsgFunctionClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_systems_item_view, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, final int i) {
        try {
            final SystemMessage.ResponseBean bean = list.get(i);
            final SystemsAdapter.ViewHolder holder = (ViewHolder) viewHolder;
            //content
            holder.systemTitle.setText(bean.getTitle());
            //content
            holder.txt_sys_content.setText(bean.getContent());
            //time
            String time = bean.getAdd_time();
            holder.txt_time.setText(DateUtils.ConvertDate(time));
            //没有跳转逻辑的消息，不显示有箭头
            if (bean.getObjType() == 0 ||
                    bean.getObjType() == Constant.MESSAGE_LOGIN ||
                    bean.getObjType() == Constant.MESSAGE_V_SUCCESS ||
                    bean.getObjType() == Constant.MESSAGE_ATTENTION ||
                    bean.getObjType() == Constant.MESSAGE_REMOVE_PENPAL) {
                holder.nextIcon.setVisibility(View.GONE);
            } else {
                holder.nextIcon.setVisibility(View.VISIBLE);
            }
            holder.txt_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemDeleteClick(i);
                    }
                }
            });
            holder.layout_sys_message.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    switch (bean.getObjType()) {
                        /*
                        case Constant.MESSAGE_LETTER://私信
                        case Constant.MESSAGE_AT://@
                        case Constant.MESSAGE_PRAISE://赞
                        case Constant.MESSAGE_COMMENT://评论
                        不在此列表展示
                         */
                        case Constant.MESSAGE_ARTICLE://关注人发布文章
                        case Constant.MESSAGE_CONTRAIBUTE_SUCCESS://文章审核通过
                            intent.setClass(context, SingleArticleDetailsActivity.class);
                            ArticleDetaisRequest detaisRequest = new ArticleDetaisRequest();
                            detaisRequest.setContribute_id(bean.getObjId());
                            detaisRequest.setType(Constant.FROM_HISTORY);
                            detaisRequest.setTopic_id(0);
                            intent.putExtra(Constant.ARTICLE_DETAILS_REQUEST, detaisRequest);
                            context.startActivity(intent);
                            break;
                        case Constant.MESSAGE_EVENT://登录送卡
                            intent.setClass(context, CardBagActivity.class);
                            context.startActivity(intent);
                            break;
                        case Constant.MESSAGE_PENPAL://加入笔友会、移交会长（副会长）
                            intent.setClass(context, PenpalDetailActivity.class);
                            intent.putExtra(Constant.PENPAL_ID, bean.getObjId());
                            context.startActivity(intent);
                            break;
                        case Constant.MESSAGE_CONTRIBUTE_FAILD:
                        case Constant.MESSAGE_REPORT:
                        case Constant.MESSAGE_FEED_BACK:
                            intent.setClass(context, SystemMessageDetailsActivity.class);
                            intent.putExtra(Constant.INTENT_TYPE, Constant.MESSAGE_CONTRIBUTE_FAILD);
                            intent.putExtra(Constant.INTENT_MESSAGE, bean.getContent());
                            context.startActivity(intent);
                            break;
                        case Constant.MESSAGE_V_FAILD:
                            intent.setClass(context, AddVCertificationActivity.class);
                            context.startActivity(intent);
                            break;
                        case Constant.MESSAGE_ATTENTION://被关注
                            intent.setClass(context, FansActivity.class);
                            context.startActivity(intent);
                            break;
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.menuView)
        SwipeMenuView menuView;
        @BindView(R.id.txt_delete)
        TextView txt_delete;
        @BindView(R.id.layout_sys_message)
        LinearLayout layout_sys_message;
        @BindView(R.id.txt_time)
        TextView txt_time;
        @BindView(R.id.txt_sys_content)
        TextView txt_sys_content;
        @BindView(R.id.next_icon)
        ImageView nextIcon;
        @BindView(R.id.system_title)
        TextView systemTitle;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
